﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CsharpTpl.model;
using XPTable.Models;
using XPTable.Renderers;
using XPTable.Editors;
using Lephone.Data;

namespace CsharpTpl.window
{
    public partial class FrmDMLB : Form
    {
        static readonly int width_min = 248;
        static readonly int width_max = 835;
        int MAX_SXH = 30;
        long _ID;
        bool _MODIFY = false;
        public FrmDMLB()
        {
            InitializeComponent();
            _LoadDMList();
            ImageList ilist = new ImageList();
            ilist.ImageSize = new Size(1, 20);
            listView1.SmallImageList = ilist;

        }
        void _LoadDMList()
        {
            listView1.Items.Clear();
            List<T_DMBMC> dmbmcs = T_DMBMC.FindAll(new OrderBy("DMJX"));
            ListViewItem item;
            foreach (T_DMBMC dmbmc in dmbmcs)
            {
                item = new ListViewItem(dmbmc.Id.ToString());
                item.SubItems.Add(dmbmc.DMJX);
                item.SubItems.Add(dmbmc.DMMC);
                listView1.Items.Add(item);
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            this.table1.BeginUpdate();

            table1.GridLines = GridLines.Both;
            table1.SelectionStyle = SelectionStyle.ListView;
            table1.EditStartAction = EditStartAction.SingleClick;
            columnHeader1.Width = 50;
            columnHeader2.Width = 60;
            columnHeader3.Width = 400;
            table1.HeaderRenderer = new GradientHeaderRenderer();
            columnModel1.Columns.Clear();
            columnModel1.Columns.Add(new TextColumn("序号"));
            columnModel1.Columns.Add(new TextColumn("名称")); 
            columnModel1.Columns.Add(new ComboBoxColumn("字段"));
            columnModel1.Columns.Add(new ComboBoxColumn("数据类型"));
            columnModel1.Columns.Add(new ComboBoxColumn("控件类型"));
            columnModel1.Columns.Add(new ComboBoxColumn("代码类型"));
            columnModel1.Columns.Add(new TextColumn("代码"));

            tableModel1.Rows.Add(new Row(
                new Cell[] {
                new Cell("10"),
                new Cell("姓名"),
                new Cell("TEXTA"),
                new Cell("字符"),
                new Cell("下拉列表"),
                new Cell("SQL"),
                new Cell("")
            }));
            tableModel1.RowHeight = 20;
            this.table1.EndUpdate();
            btnAddRow.Click += new EventHandler(btnAddNew_Click);
            btnDelRow.Click += new EventHandler(btnDelRow_Click);
            btnAdd.Click += new EventHandler(btnAdd_Click);
            table1.BeginEditing += new XPTable.Events.CellEditEventHandler(table1_BeginEditing);
            Width = width_min;
        }

        void table1_BeginEditing(object sender, XPTable.Events.CellEditEventArgs e)
        {
            if (e.Editor is ComboBoxCellEditor)
            {
                if (e.Column == 2)
                {
                    ((ComboBoxCellEditor)e.Editor).Items.Clear();
                    ((ComboBoxCellEditor)e.Editor).Items.AddRange(new string[] { "VALUE", "SXH", "TEXTA", "TEXTB", "TEXTC", "TEXTD", "TEXTE", "ZT" });
                }
                else if (e.Column ==3)
                {
                    ((ComboBoxCellEditor)e.Editor).Items.Clear();
                    ((ComboBoxCellEditor)e.Editor).Items.AddRange(new string[] { "字符","数字" });
                }
                else if (e.Column==4)
                {
                    ((ComboBoxCellEditor)e.Editor).Items.Clear();
                    ((ComboBoxCellEditor)e.Editor).Items.AddRange(new string[] { "文本框", "下拉框" ,"日期"});
                }
                else if (e.Column == 5)
                {
                    ((ComboBoxCellEditor)e.Editor).Items.Clear();
                    ((ComboBoxCellEditor)e.Editor).Items.AddRange(new string[] { "无","自定义","SQL" });
                }
            }
        }
        //新增一个代码
        void btnAdd_Click(object sender, EventArgs e)
        {
            Width = width_max;
            tboxDMJX.Clear();
            tboxDMMC.Clear();
            tboxDMMC.Focus();
            table1.BeginUpdate();
            tableModel1.Rows.Clear();
            tableModel1.Rows.Add(new Row(
                new Cell[] {
                new Cell("10"),
                new Cell("名称"),
                new Cell("DMMC"),
                new Cell("字符"),
                new Cell("文本框"),
                new Cell("无"),
                new Cell("")
            }));
            tableModel1.Rows.Add(new Row(
                new Cell[] {
                new Cell("20"),
                new Cell("值"),
                new Cell("VALUE"),
                new Cell("字符"),
                new Cell("文本框"),
                new Cell("无"),
                new Cell("")
            }));
            tableModel1.Rows.Add(new Row(
                new Cell[] {
                new Cell("100"),
                new Cell("顺序号"),
                new Cell("SXH"),
                new Cell("数字"),
                new Cell("文本框"),
                new Cell("无"),
                new Cell("")
            }));
            table1.EndUpdate();
            _MODIFY = false;
        }

        void btnDelRow_Click(object sender, EventArgs e)
        {
            table1.BeginUpdate();
            if (tableModel1.Selections.SelectedItems.Count() > 0)
            {
                tableModel1.Rows.Remove(tableModel1.Selections.SelectedItems.First());
            }

            table1.EndUpdate();
        }

        void btnAddNew_Click(object sender, EventArgs e)
        {
            if (tableModel1.Rows.Count > 8)
            {
                MessageBox.Show("最多只能设置9行");
                return;
            }
            table1.BeginUpdate();
            tableModel1.Rows.Add(new Row(
                new Cell[] {
                new Cell(MAX_SXH.ToString()),
                new Cell(""),
                new Cell(""),
                new Cell(""),
                new Cell(""),
                new Cell(""),
                new Cell("")
            }));
            MAX_SXH += 10;
            this.table1.EndUpdate();
        }
        /************************************************************************/
        /* 保存代码表定义                                                                     */
        /************************************************************************/
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!_MODIFY && T_DMBMC.FindOne(CK.K["DMJX"] == tboxDMJX.Text.Trim()) != null)
            {
                MessageBox.Show("该代码简写已经存在,请选择一个不同的简写!");
                tboxDMJX.Focus();
                return;
            }
            List<WHZD> whzds = new List<WHZD>();
            WHZD whzd;
            foreach(Row r in tableModel1.Rows)
            {
                whzd = new WHZD();
                whzd.XH = r.Cells[0].Text;
                whzd.MC = r.Cells[1].Text;
                whzd.ZD = r.Cells[2].Text;
                whzd.SJLX = r.Cells[3].Text;
                whzd.KJLX = r.Cells[4].Text;
                whzd.DMLX = r.Cells[5].Text;
                whzd.DM = r.Cells[6].Text;
                whzds.Add(whzd);
                //Console.Write(r.Cells[0].ToString());
            }
            T_DMBMC dmbmc;
            if (_MODIFY)
            {
                dmbmc = T_DMBMC.FindById(_ID);
            }
            else
            {
                dmbmc = T_DMBMC.New;
            }
            
            dmbmc.DMMC = tboxDMMC.Text;
            dmbmc.DMJX = tboxDMJX.Text;
            dmbmc.WHZD = LitJson.JsonMapper.ToJson(whzds);
            dmbmc.Save();
            btnCancel.PerformClick();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Width = width_min;
            _LoadDMList();
        }
        void _LoadWHZD(string whzd)
        {
            if (string.IsNullOrEmpty(whzd)) return;
            table1.BeginUpdate();
            tableModel1.Rows.Clear();
            List<WHZD> whzds = LitJson.JsonMapper.ToObject <List<WHZD>>(whzd);
            foreach (WHZD zd in whzds)
            {
                tableModel1.Rows.Add(new Row(
                new Cell[] {
                new Cell(zd.XH),
                new Cell(zd.MC),
                new Cell(zd.ZD),
                new Cell(zd.SJLX),
                new Cell(zd.KJLX),
                new Cell(zd.DMLX),
                new Cell(zd.DM)
            }));
            }
            table1.EndUpdate();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                btnDelete.Enabled = true;
                ListViewItem item = listView1.SelectedItems[0];
                _ID = long.Parse(item.Text);
                T_DMBMC dmbmc = T_DMBMC.FindById(_ID);
                if (dmbmc != null)
                {
                    tboxDMMC.Text = dmbmc.DMMC;
                    tboxDMJX.Text = dmbmc.DMJX;
                    _LoadWHZD(dmbmc.WHZD);
                    Width = width_max;
                    _MODIFY = true;
                }
            }
            else
            {
                btnDelete.Enabled = true;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                if (MessageBox.Show(this, "确认要删除此项记录么?此操作无法恢复.", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    ListViewItem item = listView1.SelectedItems[0];
                    T_DMBMC dmbmc = T_DMBMC.FindById(long.Parse(item.Text));
                    DbEntry.Context.ExecuteNonQuery(new Lephone.Data.SqlEntry.SqlStatement("delete from t_dmbxx where dmmc='"+dmbmc.DMJX+"'"));
                    DbEntry.Context.Delete(dmbmc);
                    _LoadDMList();
                }
                
            }
        }
    }
}
